package com.wxy.xr.servlet;

import com.wxy.xr.bean.Company;
import com.wxy.xr.bean.UploadParams;
import com.wxy.xr.util.Uploads;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/company/*")
public class CompanyServlet extends BaseServlet<Company> {

    public  void admin(HttpServletRequest request, HttpServletResponse response) throws Exception{
        request.setAttribute("companys",service.list());
        forward(request,response,"admin/company.jsp");
    }

    public  void save(HttpServletRequest request, HttpServletResponse response) throws Exception{
        UploadParams uploadParams = Uploads.parseRequest(request);

        Company company=new Company();
        BeanUtils.populate(company,uploadParams.getParams());
        if(company.getLogo()!=null && company.getLogo().length()==0){
            company.setLogo(null);
        }
        FileItem item=uploadParams.getFileParam("logoFile");
        company.setLogo(Uploads.uploadImage(item,request,company.getLogo()));

        if(service.save(company)){
            redirect(request, response,"company/admin");
        }else{
            forward(request, response,"公司信息保存失败");}
    }

    public  void remove(HttpServletRequest request, HttpServletResponse response) throws Exception{
        String[] idstrs=request.getParameterValues("id");
        List<Integer> ids=new ArrayList<>();
        for(String idstr:idstrs){
            ids.add(Integer.valueOf(idstr));
        }

        if(service.remove(ids)){
            redirect(request, response,"company/admin");
        }else{
            forward(request, response,"公司信息删除失败");
        }
    }
}
